#!/usr/bin/env node

const mysql = require('mysql2/promise');
const http = require('http');

const con = mysql.createPool({
    host: 'localhost',
    user: 'suziyi',
    password: 'ddd',
    database: 'northwind',
});

http.createServer(async (req,res) => {
    console.log(req.url);
    //router
    const table = req.url.slice(1);
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.end(await select(table));
}).listen(3000);

async function select(table) {
    const [rows] = await con.query(`select * from ${table}`);
    return JSON.stringify(rows);
}

process.on('exit', () => {
    con.end();
});

